package cn.hxh.page.controller;

import cn.hxh.page.pojo.Config;
import cn.hxh.page.pojo.Memo;
import cn.hxh.page.pojo.User;
import cn.hxh.page.service.MemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: HongXiaoHan
 * @Description
 * @Date: 2020/10/6 21:15
 */
//备忘录
@Controller
public class MemoController {

    @Autowired
    private MemoService memoService;

    @ResponseBody
    @RequestMapping("/memo/insert")
    public int memoInsert(@RequestParam("content") String content,
                          HttpServletRequest request) {
        int i = 0;
        try {
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute(Config.USER_INFO);
            memoService.memoInsert(content, user.getId());
            i = 1;
        } catch (Exception e) {
            System.out.println("失败");
            i = 0;
        }
        return i;
    }

    @ResponseBody
    @RequestMapping("/memo/selectMemoAll")
    public List<Memo> selectMemoAll(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(Config.USER_INFO);
        List<Memo> memos = memoService.selectMemoAll(user.getId());
        return memos;
    }

    @ResponseBody
    @RequestMapping("/memo/delete")
    public int delete(HttpServletRequest request, @RequestParam("content") String content,
                      @RequestParam("time") String time) {
        int i = 0;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZZZ");
            Date date = new Date();
            date = sdf.parse(time);
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute(Config.USER_INFO);
            memoService.delete(user.getId(), content, date);
            i = 1;
        } catch (Exception e) {
            i = 0;
        }
        return i;
    }

    @ResponseBody
    @RequestMapping("/memo/top")
    public int top(HttpServletRequest request, @RequestParam("content") String content,
                   @RequestParam("time") String time) {
        int flag;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZZZ");
        Date date = new Date();
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(Config.USER_INFO);
        flag = memoService.top(user.getId(), content, date);
        return flag;
    }

    @ResponseBody
    @RequestMapping("/memo/noTop")
    public int noTop(HttpServletRequest request, @RequestParam("content") String content,
                     @RequestParam("time") String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZZZ");
        Date date = new Date();
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(Config.USER_INFO);
        int flag = memoService.noTop(user.getId(), content, date);
        return flag;
    }


    @ResponseBody
    @RequestMapping("/memo/selectTop")
    public List<Memo> selectTop(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(Config.USER_INFO);
        List<Memo> memos = memoService.selectTop(user.getId());
        return memos;
    }
}
